<template>
  <el-form :model="form" :rules="rules" ref="formRef">
    <el-form-item label="门店名称" prop="name" :label-width="formLabelWidth">
      <el-input v-model="form.name"></el-input>
    </el-form-item>
    <el-form-item label="门店地址" prop="address" :label-width="formLabelWidth">
      <el-input type="textarea" :rows="4" v-model="form.address"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark" :label-width="formLabelWidth">
      <el-input type="textarea" :rows="4" v-model="form.remark"></el-input>
    </el-form-item>
    <el-form-item :label-width="formLabelWidth">
      <el-button plain @click="resetForm('formRef')">取消</el-button>
      <el-button type="primary" @click="submitForm('formRef')">确定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  data() {
    return {
      form: {
        name: "",
        address: "",
        remark:""
      },
      formLabelWidth: "150px",
      rules: {
        name: [
          { required: true, message: "请输入门店名称", trigger: "blur" },
          {  max: 50, message: '长度不能超过50个字符', trigger: 'blur' }
        ],
        address: [
          { required: true, message: "请输入门店地址", trigger: "blur" },
          {  max: 50, message: '长度不能超过50个字符', trigger: 'blur' }
        ],
        remark: [
          { required: true, message: "请输入备注", trigger: "blur" },
          {  max: 50, message: '长度不能超过50个字符', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.form.name = this.form.name.replace(/^\s+|\s+$/g, "");
      this.form.address = this.form.address.replace(/^\s+|\s+$/g, "");
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$service.addStore(this.form).then(res => {
            if (res.code == "H0900") {
              this.$message.error(res.message);
            } else {
              this.$message({
                message: "添加成功",
                type: "success"
              });
              this.$refs[formName].resetFields();
              this.$emit("submit", false);
            }
          });
        }
      });
    },
    resetForm(formName) {
      this.$emit("submit", false);
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>